#Rem
	header: TILED Rendering
#End

Strict
Import mojo
Import agfx

#Rem
summary: TTiled_Renderer
#End
Class TTiled_Renderer

	Private

	#Rem
	summary: tiled map data reference
	#End
	Field _map_data:		TTiled_Map
	
	#Rem
	summary: user defined orders for rendering layers from TILED data
	#End
	Field render_layers:	TTiled_RenderOrder

	Public
	
	#Rem
	summary: Constructor
	mapdata:TTiled_Map	pointer to tiled map data
	#End
	Method New(mapdata:TTiled_Map)
		Self._map_data = mapdata
		Self.render_layers = New TTiled_RenderOrder()
	End


	#Rem
	summary: set rendering position for all layers
	ox:Int	- position x
	oy:Int	- position y
	#End
	Method SetPosition:Void(ox:Int,oy:Int)
		Self._map_data.SetPosition(ox,oy)
	End
	#Rem
	summary: Add render layer to order list
	layer_name:String	layername from level
	#End
	Method AddRenderLayersOrder:Void(layer_name:String)
	
		If DEBUG Then Print("Find layer: "+layer_name)

		Local tid:Int = Self._map_data.GetLayerIDbyName(layer_name)		
		If tid>=0 Then Self.render_layers.Add(P2D_LAYER_TYPE_TILESET,tid)
		
		Local oid:Int = Self._map_data.GetObjectGroupIDbyName(layer_name)
		If oid>=0 Then Self.render_layers.Add(P2D_LAYER_TYPE_OBJECTS,oid)
		
		If DEBUG Then Print(" ... done")
	End Method

	#Rem
	summary: Update
	- call this in app. OnUpdate() loop
	#End
	Method Update:Void()
		
	End Method
	
	#Rem
	summary: Render
	- call this in app. OnRender() loop
	#End
	Method Render:Void()
		Local order:TRenderOrder
		
		
		For Local i:Int = 0 To Self.render_layers.orders.Size()-1
		order = Self.render_layers.orders.Get(i)
		'For order = EachIn Self.render_layers.orders
			Select order.type
				Case P2D_LAYER_TYPE_TILESET
					Self._map_data.DrawLayer(order.id)
				Case P2D_LAYER_TYPE_OBJECTS
					Self._map_data.DrawObjectGroup(order.id)
			End Select
		Next
	
	End Method

End